new Vue({
    el: '#app',
    data: {
        fileList: [],
        path: '请选择文件或文件夹',
        link: '请选择文件或文件夹',
    },
    mounted() {
        this.dirData()
    },
    methods: {
        // 获取目录数据
        dirData() {
            const _this = this;
            $.ajax({
                url: 'file/directoryTree?rnd=' + Math.random(),
                type: 'GET',
                async: true,
                success: function (res) {
                    let treeData = _this.dirDataFormat(res.treeData)
                    _this.initTree(treeData);
                }
            });
        },

        // 格式化树结构
        dirDataFormat(node) {
            let result = {
                text: node.label,
                path: node.path,
                nodes: [],
                tags: []
            };

            let children = node.children;
            if (children != null && children.length > 0) {
                for (let i = 0; i < children.length; i++) {
                    result.nodes.push(this.dirDataFormat(children[i]));
                }
                result.tags.push(children.length)
            }

            return result;

        },

        // 初始化树结构
        initTree(treeData) {
            let _this = this;
            $('#dirTree').treeview({
                data: [treeData],
                showTags: true,
                onNodeSelected: function (event, node) {
                    _this.pathFormat(node.path);
                    _this.dirFileList(node.path);
                },
            });
        },

        // 目录文件列表
        dirFileList(path) {
            const _this = this;
            $.ajax({
                url: 'file/fileList?rnd=' + Math.random(),
                type: 'GET',
                data: {directory: path},
                success: function (res) {
                    _this.fileList = res;
                }
            });
        },

        // 文件双击
        preview(path, size) {
            // 如果是目录就可以双击
            if (size == '-') {
                this.pathFormat(path);
                this.dirFileList(path);
            }
        },

        // 导航路径
        pathFormat(path) {
            let _this = this;
            _this.path = path;
            let link = decrypt(path).toString();
            let index = link.indexOf("/");
            _this.link = link.substring(index + 1, link.length);
        },

        // 返回上一级
        backup() {
            let path = decrypt(this.path).toString();
            let index = path.lastIndexOf("/")
            path = path.substring(0, index);

            let lIndex = path.indexOf("/");
            this.link = path.substring(lIndex + 1, path.length);

            path = encrypt(path);
            this.path = path;

            this.dirFileList(path)
        },

        // 文件下载
        download(path) {
            if (confirm('是否下载？')) {
                $.get('file/download?rnd=' + Math.random(), {path: path})
            }
        },

        // 文件删除
        delFile(path) {
            let _this = this;
            if (confirm('是否删除？')) {
                this.path = path;
                $.ajax({
                    url: 'file/delete?rnd=' + Math.random(),
                    type: 'DELETE',
                    data: {path: path},
                    success: function (res) {
                        if (res) {
                            toastr.success("删除成功！")
                            _this.backup();
                            _this.dirData();
                        }
                    }
                });

            }
        },

        // 文件更名
        reName(path) {
            let _this = this;
            let newName = prompt('请输入文件名');
            if (newName) {
                this.path = path;
                $.ajax({
                    url: 'file/update?rnd=' + Math.random(),
                    type: 'PUT',
                    data: {path: path, newFile: newName},
                    success: function (res) {
                        if (res) {
                            toastr.success("修改成功！")
                            _this.backup();
                            _this.dirData();
                        }
                    }
                });
            }
        },

        // 创建目录
        createDir() {
            let _this = this;
            let dirName = prompt('请输入目录名称');
            if (dirName) {
                $.ajax({
                    url: 'file/createDir?rnd=' + Math.random(),
                    type: 'POST',
                    data: {path: this.path, dirName: dirName},
                    success: function (res) {
                        if (res) {
                            toastr.success("新建成功！")
                            _this.dirData();
                            _this.dirFileList(_this.path)
                        }
                    }
                });

            }

        }


    }
})